// import VueI18n from 'vue-i18n';

// console.log(VueI18n)
// import Vue from "vue/types/index";
const imageTypes = ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'psd', 'svg', 'tiff'];
const videoTypes = ['avi', 'wmv', 'mpeg', 'mp4', 'm4v', 'mov', 'asf', 'flv', 'f4v', 'rmvb', 'rm', '3gp', 'vob', 'video'];

let util = {
	getDateDiff: function (startTime, endTime, diffType) {
		//将xxxx-xx-xx的时间格式，转换为 xxxx/xx/xx的格式
		startTime = startTime.replace(/\-/g, '/');
		endTime = endTime.replace(/\-/g, '/');

		//将计算间隔类性字符转换为小写
		diffType = diffType.toLowerCase();
		let sTime = new Date(startTime); //开始时间
		let eTime = new Date(endTime); //结束时间
		//作为除数的数字
		let divNum = 1;
		switch (diffType) {
			case 'second':
				divNum = 1000;
				break;
			case 'minute':
				divNum = 1000 * 60;
				break;
			case 'hour':
				divNum = 1000 * 3600;
				break;
			case 'day':
				divNum = 1000 * 3600 * 24;
				break;
			default:
				break;
		}
		return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(divNum));
	},
	getEnv: function () {
		const isTong = process.env.NODE_ENV === 'tong';
		let ksiot = '/kakone';
		if (isTong) {
			ksiot = '';
		}
		return ksiot;
	},
	/**
	 * 根据文件后缀判断文件类型，image，video，file
	 */
	fileType: function (fileName) {
		const index = fileName.lastIndexOf('.');
		const ext = fileName.substring(index + 1);

		if (imageTypes.indexOf(ext.toLowerCase()) >= 0) {
			return 'image';
		} else if (videoTypes.indexOf(ext.toLowerCase()) >= 0) {
			return 'video';
		} else {
			return 'file';
		}
	},
	filePathToUrl(path) {
		if (!path) {
			return '';
		}
		let filePathPre = process.env.VUE_APP_FILE_PATH_PRE;

		let fileRoutePre = process.env.VUE_APP_FILE_ROUTE_PRE;
		path = path.replace(filePathPre, fileRoutePre);

		return path;
	},
	autoWidth: function (w1, w2) {
		const language = localStorage.getItem('language');
		let w = 0;
		switch (language) {
			case 'zh_CN':
				w = w1;
				break;
			case 'en_US':
				w = w2;
				break;
		}
		return w;
	},
	thousandth: function (value) {
		if (!value) return '';
		return String(value).replace(/\B(?<!(\.\d+))(?=(\d{3})+\b)/g, ',');
	},
	// 随机生成编码 len编码长度
	getUuid(len, radix) {
		var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
		var uuid = [],
			i;
		radix = radix || chars.length;

		if (len) {
			// Compact form
			for (i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)];
		} else {
			// rfc4122, version 4 form
			var r;
			// rfc4122 requires these characters
			//   uuid[8] = uuid[13] = uuid[18] = uuid[23] = "-";
			uuid[14] = '4';

			// Fill in random data.  At i==19 set the high bits of clock sequence as
			// per rfc4122, sec. 4.1.5
			for (i = 0; i < 36; i++) {
				if (!uuid[i]) {
					r = 0 | (Math.random() * 16);
					uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r];
				}
			}
		}

		return uuid.join('');
	},
	numberFormatNoFixed(num, decimal) {
		num = num.toString();
		let index = num.indexOf('.');
		if (index !== -1) {
			num = num.substring(0, decimal + index + 1);
		} else {
			num = num.substring(0);
		}
		const formatWithRegex = (number) => {
			return !(number + '').includes('.')
				? // 就是说1-3位后面一定要匹配3位
				  (number + '').replace(/\d{1,7}(?=(\d{3})+$)/g, (match) => {
						return match;
				  })
				: (number + '').replace(/\d{1,7}(?=(\d{3})+(\.))/g, (match) => {
						return match;
				  });
		};
		return formatWithRegex(parseFloat(num).toFixed(decimal));
		// const number = parseFloat(parseFloat(value));
		// console.log("value", number);

		// const dem = Math.pow(10, decimals);
		// console.log(Math.floor(number * dem) / dem);
		// return (Math.floor(number * dem) / dem).toFixed(decimals);
	},
};

export default util;
